"use strict";

/**
 * @auther wtfssd
 * Created by wtfssd on 2018/1/11
 */
import * as React from 'react'
import {commonProps, commonStyles} from "../../assets/styles/common";
import NavBackButton from "../../component/BackButton";
import {openUrl} from "../../utils/tools";

const {View, Alert, Platform, FlatList, TouchableOpacity} = require('react-native');
const {connect} = require('react-redux');

const NavigationBar = require('react-native-navbar').default;
const {Actions} = require("react-native-router-flux");
const Contacts = require('react-native-contacts');
import {Contact} from 'react-native-contacts';
import MyText from "../../component/MyText";
import MyIcon from "../../component/icon";
import Request from "../../utils/Request";

class ContactSelectPage extends React.Component<any, any> {

    state = {
        contacts: []
    };
//     Contacts.select( (err, contact) => {
//     console.log('sele', err, contact);
//     if (err) {
//         alert(err);
//     } else {
//     // this.setState({
//     //     firstName: contact.familyName,
//     //     lastName: contact.givenName,
//     // })
// }
// })


    componentDidMount(): void {
        Contacts.checkPermission((err, permission) => {
            // console.log('检查权限', err, permission);
            if (permission === 'undefined') {
                Contacts.requestPermission((err, permission) => {
                    if (permission === 'authorized') {
                        this.getAll();
                    } else {
                        Actions.pop();
                    }
                })
            } else {
                if (permission !== 'authorized') {
                    this.alertNoPermission();
                } else {
                    this.getAll();
                }
            }

        })
    }


    getAll = () => {
        Contacts.getAll((err, contacts: Contact[]) => {
            Request.log('获取所有联系人', contacts);
            this.setState({contacts})
        })
    };
    //提示没有权限
    alertNoPermission = () => {
        Alert.alert(
            '警告',
            '暂无通讯录权限,请前往设置->隐私->通讯录->火星空间 打开通讯录权限',
            [
                {text: '取消'},
                {text: '确定'},
            ],
        )
    };


    onSelect = (c) => {
        if (this.props.onSelect) {
            this.props.onSelect(c);
        }
        Actions.pop();
    };
    renderItem = (p) => {
        const c: Contact = p.item;


        let text = c.familyName + c.givenName + ' ' + c.company;

        return (
            <TouchableOpacity onPress={this.onSelect.bind(this, c)}
                              style={[commonStyles.listItem, commonStyles.underLine, {
                                  justifyContent: 'space-between',
                                  padding: 10
                              }]}>
                <MyText>{text}</MyText>
                <MyIcon name={'icon-iconfontyoujiantou'}/>
            </TouchableOpacity>
        )
    };

    render() {
        return (
            <View style={commonStyles.container}>
                <NavigationBar {...commonProps.navBar}
                               leftButton={<NavBackButton/>}
                               title={{title: '选择联系人', tintColor: '#fff'}}
                />
                <FlatList data={this.state.contacts}
                          renderItem={this.renderItem}
                          keyExtrator={(_, index) => index}

                />
            </View>
        );
    }
}

export default ContactSelectPage;